package com.luxuewen.sxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luxuewen.sxa.common.enums.ResponseEnum;
import com.luxuewen.sxa.common.exception.ApiException;
import com.luxuewen.sxa.entity.SxaApis;
import com.luxuewen.sxa.entity.SxaRoleApis;
import com.luxuewen.sxa.entity.SxaRoles;
import com.luxuewen.sxa.service.SxaApisService;
import com.luxuewen.sxa.mapper.SxaApisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wen
* @description 针对表【sxa_apis】的数据库操作Service实现
* @createDate 2023-11-01 16:28:59
*/
@Service
public class SxaApisServiceImpl extends ServiceImpl<SxaApisMapper, SxaApis>
    implements SxaApisService{
    @Autowired
    private SxaRoleApisServiceImpl sxaRoleApisService;

    /**
     * 接口权限验证
     * @param roleIds 用户角色
     * @param alias 接口别名
     */
    public void apiAuth(List<Integer> roleIds, String alias) {
        // 后台是否有配置该接口
//        QueryWrapper<SxaApis> sxaApisQueryWrapper = new QueryWrapper<>();
//        sxaApisQueryWrapper.eq("alias", alias).select("id");
//        List<SxaApis> sxaApisList = this.list(sxaApisQueryWrapper);
//        if (sxaApisList == null) {
//            throw new ApiException(ResponseEnum.ACCESS_DENIED.getCode(), "后台未配置当前接口，请联系管理员配置！");
//        }

        QueryWrapper<SxaRoleApis> sxaRoleApisQueryWrapper = new QueryWrapper<>();
        sxaRoleApisQueryWrapper.inSql("api_id", "select id from sxa_apis where alias='"+alias+"'")
                .in("role_id", roleIds);
        boolean ex = sxaRoleApisService.exists(sxaRoleApisQueryWrapper);
        if (!ex) {
            throw new ApiException(ResponseEnum.ACCESS_DENIED.getCode(), "没有访问当前接口的权限");
        }
    }

}




